// json文件转成scv
import json2csv from 'json2csv'
export const download = (datas, fields, fileName) => {
  //datas：数据，fields：字段名
  try {
    const result = json2csv.parse(datas, {
      fields: fields
    });
    // 判断浏览器类型
    if ((navigator.userAgent.indexOf('compatible') > -1 &&
      navigator.userAgent.indexOf('MSIE') > -1) ||
      navigator.userAgent.indexOf('Edge') > -1) {
      //IE10或Edge浏览器
      var BOM = "\uFEFF";
      var csvData = new Blob([BOM + result], { type: "text/csv" });
      navigator.msSaveBlob(csvData, fileName);
    } else {
      //非IE浏览器
      var csvContent = "data:text/csv;charset=utf-8,\uFEFF" + result;
      //使用a标签的download属性实现下载功能
      var link = document.createElement("a");
      link.href = encodeURI(csvContent);
      link.download = fileName;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  } catch (err) {
    alert(err);
  }
}

